{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[30,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入字符串\"1234\"\n",
      "Counter({'\"': 2, '1': 1, '2': 1, '3': 1, '4': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "text = input(\"请输入字符串\")\n",
    "frequencies = Counter(text)\n",
    "print(frequencies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串：1,3,2,1,6\n"
     ]
    }
   ],
   "source": [
    "a = input('请输入一个字符串：')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bcchsha char:a,last site:7 char:b,last site:1 char:c,last site:3 char:h,last site:6 char:s,last site:5\n",
      "char:a,last site:93\n",
      "char:b,last site:33\n",
      "char:c,last site:85\n",
      "char:h,last site:86\n",
      "char:s,last site:97\n"
     ]
    }
   ],
   "source": [
    "dict1={'a':0,'b':0,'c':0,'f':0,'h':0,'s':0}\n",
    "num=0\n",
    "def function(a,dict1,num):\n",
    "    for i in a:\n",
    "        num+=1\n",
    "        if i in dict1:\n",
    "            dict1[i]=num\n",
    "    return dict1\n",
    "a=str(input())\n",
    "function(a,dict1,num)\n",
    "for x in dict1:\n",
    "    if dict1[x]!=0:\n",
    "        print(\"char:{},last site:{}\".format(x,dict1[x]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:{1,2,3},{1,2},{4,5,6}\n",
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "last=eval(input('请输入一个列表:'))\n",
    "print(reduce(lambda x,y:x|y,last))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串：45612798\n",
      "no\n"
     ]
    }
   ],
   "source": [
    "str1=input('请输入一个字符串：')\n",
    "str2=str1[::-1]\n",
    "if str1==str2:\n",
    "    print('yes')\n",
    "else:\n",
    "    print('no')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Python 3.x的range()函数返回一个range可迭代对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.表达式“[3]in [1,2,3,4]”的值为4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.列表对象的 sort()方法用来对列表元素进行原地排序,该函数返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.列表对象的remove方法删除首次出现的指定元素,如果列表中不存在要删除的元素，则抛出异常。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.假设列表对象aList的值为[3,4,5,6,7,9,11,13,15,17],那么切片aList[3:7]得的值是6，7,9，11]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.在 Python 中,字典和集合都是用一对[]作为界定符,字典的每个元素由两部分组成，即键_和值,其中键不允许重复。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.使用字典对象的 items(方法可以返回字典的“键:值”对，使用字典对象的 keys()方法可以返回字典的“键”,使用字典对象的 valuex方法可以返回字典的“值”"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.假设有列表 a=['name','age', 'sex']和 b=['Dong',38,'Male'],使用一条语句将这两个列表的内容转换为字典,并且以列表a中的元素为“键”,以列表b中的元素为“值”,这个语句可以写为c=dict(zipla,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.假设有一个列表a,现要求从列表a中每3个元素取1个，并且将取到的元素组成新的列表b,可以使用语句b=a[::3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.使用列表推导式生成包含10个数字5的列表,语句可以写为[5 for i in range(10)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11. 不可以(可以、不可以)使用del命令来删除元组中的部分元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12.已知列表data中有若干字符串,要求编写程序,对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['www.com', 'www.cn', '@qq.com']\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter     #12题\n",
    "data = [\"www.com\",\"www.cn\",\"@www\",\"@qq.com\"]\n",
    "for d in data:\n",
    "    res=Counter(d)\n",
    "    max_len=0\n",
    "    for key in res:\n",
    "        if max_len<res[key]:\n",
    "            max_len=res[key]\n",
    "    n=len(d)\n",
    "    if max_len>n/2:\n",
    "        data.remove(d)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.编写程序,使用筛选法查找并输出小于1000的所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[76, 95, 60, 62, 91, 93, 83, 64, 99, 86, 76, 86, 98, 33, 46, 93, 7, 100, 75, 59, 95, 25, 10, 31, 76, 77, 30, 62, 36, 19, 53, 99, 77, 70, 94, 79, 11, 19, 66, 0, 72, 29, 3, 59, 54, 98, 40, 29, 77, 21, 71, 89, 45, 4, 26, 36, 70, 47, 48, 12, 28, 6, 41, 77, 91, 70, 39, 36, 30, 12, 37, 13, 51, 20, 32, 41, 11, 53, 93, 87, 97, 45, 52, 37, 66, 86, 99, 0, 52, 34, 66, 17, 65, 44, 8, 11, 59, 42, 7, 6, 22, 100, 16, 7, 24, 22, 52, 81, 0, 16, 33, 44, 7, 45, 13, 43, 98, 59, 8, 14, 37, 58, 80, 73, 39, 32, 40, 32, 95, 29, 85, 33, 61, 26, 19, 57, 43, 18, 52, 83, 42, 2, 57, 59, 28, 3, 66, 19, 79, 87, 86, 7, 53, 43, 25, 52, 64, 8, 23, 24, 70, 14, 71, 86, 39, 52, 87, 77, 42, 82, 2, 9, 99, 85, 17, 7, 59, 23, 93, 15, 9, 42, 83, 24, 17, 29, 78, 83, 8, 49, 85, 0, 73, 51, 24, 77, 40, 9, 2, 73, 17, 57, 72, 52, 20, 30, 87, 1, 97, 7, 28, 98, 70, 28, 76, 45, 48, 25, 35, 28, 20, 67, 75, 1, 85, 25, 98, 34, 18, 0, 18, 77, 14, 97, 70, 23, 24, 97, 3, 15, 10, 27, 22, 39, 74, 80, 43, 86, 91, 73, 18, 87, 93, 60, 38, 18, 18, 90, 10, 99, 2, 42, 22, 49, 43, 29, 72, 21, 90, 15, 12, 30, 21, 41, 94, 37, 52, 21, 41, 38, 56, 100, 84, 17, 28, 81, 12, 79, 35, 73, 60, 40, 0, 60, 9, 53, 28, 95, 95, 2, 15, 35, 19, 15, 93, 6, 73, 53, 10, 3, 97, 12, 49, 18, 50, 40, 60, 88, 94, 15, 61, 94, 48, 47, 26, 87, 51, 89, 100, 88, 58, 87, 5, 18, 29, 66, 35, 19, 52, 56, 56, 25, 16, 95, 84, 79, 81, 45, 97, 78, 66, 36, 38, 63, 10, 27, 77, 45, 78, 7, 51, 4, 25, 31, 13, 61, 92, 65, 7, 13, 55, 21, 71, 66, 40, 42, 97, 56, 88, 18, 66, 77, 75, 97, 90, 10, 18, 47, 29, 61, 1, 65, 84, 100, 37, 6, 88, 54, 14, 20, 40, 85, 14, 63, 26, 5, 31, 61, 64, 51, 2, 89, 33, 43, 25, 14, 69, 58, 16, 53, 84, 2, 17, 29, 54, 96, 59, 96, 34, 18, 56, 65, 78, 24, 29, 68, 41, 64, 30, 81, 16, 70, 35, 85, 52, 60, 70, 14, 24, 84, 36, 0, 54, 34, 48, 55, 62, 75, 39, 89, 36, 35, 26, 40, 40, 18, 71, 68, 83, 30, 65, 56, 20, 96, 24, 18, 38, 59, 93, 33, 35, 23, 27, 6, 44, 52, 6, 92, 91, 20, 28, 80, 18, 99, 31, 80, 9, 18, 66, 95, 66, 42, 29, 59, 49, 26, 10, 35, 57, 94, 51, 4, 12, 8, 39, 83, 37, 38, 52, 63, 7, 33, 68, 66, 93, 40, 33, 43, 77, 5, 18, 48, 81, 35, 91, 51, 24, 57, 10, 62, 44, 77, 40, 70, 69, 14, 47, 39, 86, 0, 25, 97, 39, 45, 40, 49, 65, 53, 89, 53, 70, 90, 17, 57, 48, 67, 30, 93, 34, 24, 78, 21, 73, 43, 95, 19, 67, 98, 28, 92, 14, 83, 72, 41, 96, 89, 97, 29, 51, 26, 15, 39, 22, 15, 8, 27, 25, 73, 71, 93, 86, 63, 22, 96, 78, 77, 4, 24, 35, 5, 52, 41, 8, 57, 40, 93, 26, 51, 19, 68, 32, 62, 15, 78, 97, 94, 82, 53, 3, 44, 11, 97, 8, 58, 20, 81, 89, 87, 8, 51, 39, 46, 68, 52, 99, 86, 83, 18, 98, 17, 54, 82, 5, 83, 78, 97, 83, 21, 48, 91, 92, 57, 73, 79, 3, 39, 56, 91, 49, 11, 2, 56, 65, 84, 97, 5, 94, 10, 85, 98, 71, 63, 97, 32, 50, 60, 90, 74, 47, 67, 85, 12, 49, 56, 80, 17, 92, 7, 20, 37, 77, 87, 26, 88, 86, 49, 19, 13, 48, 100, 1, 80, 80, 89, 3, 28, 79, 79, 37, 2, 68, 35, 98, 2, 92, 96, 10, 5, 62, 92, 10, 58, 83, 84, 90, 82, 83, 8, 14, 13, 63, 87, 43, 56, 40, 33, 62, 78, 41, 23, 90, 12, 87, 34, 99, 54, 1, 81, 90, 8, 41, 91, 100, 4, 61, 67, 99, 82, 89, 42, 66, 97, 70, 41, 32, 9, 59, 90, 75, 60, 92, 9, 67, 65, 11, 39, 97, 60, 21, 90, 97, 9, 63, 0, 96, 74, 32, 37, 34, 29, 100, 70, 13, 44, 85, 52, 38, 84, 78, 78, 53, 90, 72, 81, 94, 91, 95, 65, 0, 20, 24, 35, 100, 82, 79, 10, 84, 79, 13, 53, 96, 31, 38, 48, 94, 26, 40, 94, 38, 22, 3, 99, 76, 30, 25, 75, 19, 34, 60, 79, 58, 74, 2, 80, 62, 17, 91, 49, 36, 30, 54, 31, 53, 4, 5, 19, 66, 89, 72, 90, 57, 19, 1, 8, 83, 28, 88, 9, 20, 55, 60, 70, 3, 65, 31, 1, 97, 2, 84, 31, 57, 97, 39, 14, 91, 65, 45, 97, 76, 41, 49, 51, 3, 2, 94, 70, 80, 23, 10, 25, 75, 55, 52, 40, 14, 38, 54, 78, 75, 55, 52, 86, 37, 11, 75, 3, 70, 17, 86, 56, 19, 94, 13, 82, 63, 86, 84, 42, 47, 96, 32, 16, 13, 29, 22, 14, 42, 87, 90, 11, 58, 79, 44, 4, 86, 25, 86, 5, 60, 99, 77, 66, 81, 38, 38, 22, 81, 14, 77, 33, 45, 28, 1, 55, 66, 71, 67, 94, 84, 33, 52, 56, 29, 50, 6, 82, 50, 13, 47, 55, 86, 27, 65, 26, 95, 11, 47, 19, 51, 3]\n",
      "76 : 6\n",
      "95 : 10\n",
      "60 : 12\n",
      "62 : 8\n",
      "91 : 11\n",
      "93 : 11\n",
      "83 : 13\n",
      "64 : 4\n",
      "99 : 11\n",
      "86 : 16\n",
      "76 : 6\n",
      "86 : 16\n",
      "98 : 9\n",
      "33 : 10\n",
      "46 : 2\n",
      "93 : 11\n",
      "7 : 11\n",
      "100 : 9\n",
      "75 : 9\n",
      "59 : 10\n",
      "95 : 10\n",
      "25 : 12\n",
      "10 : 13\n",
      "31 : 8\n",
      "76 : 6\n",
      "77 : 15\n",
      "30 : 9\n",
      "62 : 8\n",
      "36 : 7\n",
      "19 : 14\n",
      "53 : 12\n",
      "99 : 11\n",
      "77 : 15\n",
      "70 : 15\n",
      "94 : 13\n",
      "79 : 11\n",
      "11 : 9\n",
      "19 : 14\n",
      "66 : 15\n",
      "0 : 10\n",
      "72 : 6\n",
      "29 : 14\n",
      "3 : 12\n",
      "59 : 10\n",
      "54 : 8\n",
      "98 : 9\n",
      "40 : 16\n",
      "29 : 14\n",
      "77 : 15\n",
      "21 : 8\n",
      "71 : 7\n",
      "89 : 10\n",
      "45 : 9\n",
      "4 : 7\n",
      "26 : 11\n",
      "36 : 7\n",
      "70 : 15\n",
      "47 : 8\n",
      "48 : 9\n",
      "12 : 8\n",
      "28 : 12\n",
      "6 : 7\n",
      "41 : 11\n",
      "77 : 15\n",
      "91 : 11\n",
      "70 : 15\n",
      "39 : 13\n",
      "36 : 7\n",
      "30 : 9\n",
      "12 : 8\n",
      "37 : 10\n",
      "13 : 11\n",
      "51 : 12\n",
      "20 : 10\n",
      "32 : 8\n",
      "41 : 11\n",
      "11 : 9\n",
      "53 : 12\n",
      "93 : 11\n",
      "87 : 12\n",
      "97 : 21\n",
      "45 : 9\n",
      "52 : 18\n",
      "37 : 10\n",
      "66 : 15\n",
      "86 : 16\n",
      "99 : 11\n",
      "0 : 10\n",
      "52 : 18\n",
      "34 : 8\n",
      "66 : 15\n",
      "17 : 11\n",
      "65 : 12\n",
      "44 : 7\n",
      "8 : 12\n",
      "11 : 9\n",
      "59 : 10\n",
      "42 : 10\n",
      "7 : 11\n",
      "6 : 7\n",
      "22 : 9\n",
      "100 : 9\n",
      "16 : 6\n",
      "7 : 11\n",
      "24 : 12\n",
      "22 : 9\n",
      "52 : 18\n",
      "81 : 10\n",
      "0 : 10\n",
      "16 : 6\n",
      "33 : 10\n",
      "44 : 7\n",
      "7 : 11\n",
      "45 : 9\n",
      "13 : 11\n",
      "43 : 9\n",
      "98 : 9\n",
      "59 : 10\n",
      "8 : 12\n",
      "14 : 14\n",
      "37 : 10\n",
      "58 : 7\n",
      "80 : 9\n",
      "73 : 9\n",
      "39 : 13\n",
      "32 : 8\n",
      "40 : 16\n",
      "32 : 8\n",
      "95 : 10\n",
      "29 : 14\n",
      "85 : 9\n",
      "33 : 10\n",
      "61 : 6\n",
      "26 : 11\n",
      "19 : 14\n",
      "57 : 10\n",
      "43 : 9\n",
      "18 : 17\n",
      "52 : 18\n",
      "83 : 13\n",
      "42 : 10\n",
      "2 : 13\n",
      "57 : 10\n",
      "59 : 10\n",
      "28 : 12\n",
      "3 : 12\n",
      "66 : 15\n",
      "19 : 14\n",
      "79 : 11\n",
      "87 : 12\n",
      "86 : 16\n",
      "7 : 11\n",
      "53 : 12\n",
      "43 : 9\n",
      "25 : 12\n",
      "52 : 18\n",
      "64 : 4\n",
      "8 : 12\n",
      "23 : 6\n",
      "24 : 12\n",
      "70 : 15\n",
      "14 : 14\n",
      "71 : 7\n",
      "86 : 16\n",
      "39 : 13\n",
      "52 : 18\n",
      "87 : 12\n",
      "77 : 15\n",
      "42 : 10\n",
      "82 : 8\n",
      "2 : 13\n",
      "9 : 9\n",
      "99 : 11\n",
      "85 : 9\n",
      "17 : 11\n",
      "7 : 11\n",
      "59 : 10\n",
      "23 : 6\n",
      "93 : 11\n",
      "15 : 9\n",
      "9 : 9\n",
      "42 : 10\n",
      "83 : 13\n",
      "24 : 12\n",
      "17 : 11\n",
      "29 : 14\n",
      "78 : 12\n",
      "83 : 13\n",
      "8 : 12\n",
      "49 : 10\n",
      "85 : 9\n",
      "0 : 10\n",
      "73 : 9\n",
      "51 : 12\n",
      "24 : 12\n",
      "77 : 15\n",
      "40 : 16\n",
      "9 : 9\n",
      "2 : 13\n",
      "73 : 9\n",
      "17 : 11\n",
      "57 : 10\n",
      "72 : 6\n",
      "52 : 18\n",
      "20 : 10\n",
      "30 : 9\n",
      "87 : 12\n",
      "1 : 8\n",
      "97 : 21\n",
      "7 : 11\n",
      "28 : 12\n",
      "98 : 9\n",
      "70 : 15\n",
      "28 : 12\n",
      "76 : 6\n",
      "45 : 9\n",
      "48 : 9\n",
      "25 : 12\n",
      "35 : 12\n",
      "28 : 12\n",
      "20 : 10\n",
      "67 : 7\n",
      "75 : 9\n",
      "1 : 8\n",
      "85 : 9\n",
      "25 : 12\n",
      "98 : 9\n",
      "34 : 8\n",
      "18 : 17\n",
      "0 : 10\n",
      "18 : 17\n",
      "77 : 15\n",
      "14 : 14\n",
      "97 : 21\n",
      "70 : 15\n",
      "23 : 6\n",
      "24 : 12\n",
      "97 : 21\n",
      "3 : 12\n",
      "15 : 9\n",
      "10 : 13\n",
      "27 : 5\n",
      "22 : 9\n",
      "39 : 13\n",
      "74 : 4\n",
      "80 : 9\n",
      "43 : 9\n",
      "86 : 16\n",
      "91 : 11\n",
      "73 : 9\n",
      "18 : 17\n",
      "87 : 12\n",
      "93 : 11\n",
      "60 : 12\n",
      "38 : 11\n",
      "18 : 17\n",
      "18 : 17\n",
      "90 : 13\n",
      "10 : 13\n",
      "99 : 11\n",
      "2 : 13\n",
      "42 : 10\n",
      "22 : 9\n",
      "49 : 10\n",
      "43 : 9\n",
      "29 : 14\n",
      "72 : 6\n",
      "21 : 8\n",
      "90 : 13\n",
      "15 : 9\n",
      "12 : 8\n",
      "30 : 9\n",
      "21 : 8\n",
      "41 : 11\n",
      "94 : 13\n",
      "37 : 10\n",
      "52 : 18\n",
      "21 : 8\n",
      "41 : 11\n",
      "38 : 11\n",
      "56 : 12\n",
      "100 : 9\n",
      "84 : 12\n",
      "17 : 11\n",
      "28 : 12\n",
      "81 : 10\n",
      "12 : 8\n",
      "79 : 11\n",
      "35 : 12\n",
      "73 : 9\n",
      "60 : 12\n",
      "40 : 16\n",
      "0 : 10\n",
      "60 : 12\n",
      "9 : 9\n",
      "53 : 12\n",
      "28 : 12\n",
      "95 : 10\n",
      "95 : 10\n",
      "2 : 13\n",
      "15 : 9\n",
      "35 : 12\n",
      "19 : 14\n",
      "15 : 9\n",
      "93 : 11\n",
      "6 : 7\n",
      "73 : 9\n",
      "53 : 12\n",
      "10 : 13\n",
      "3 : 12\n",
      "97 : 21\n",
      "12 : 8\n",
      "49 : 10\n",
      "18 : 17\n",
      "50 : 4\n",
      "40 : 16\n",
      "60 : 12\n",
      "88 : 6\n",
      "94 : 13\n",
      "15 : 9\n",
      "61 : 6\n",
      "94 : 13\n",
      "48 : 9\n",
      "47 : 8\n",
      "26 : 11\n",
      "87 : 12\n",
      "51 : 12\n",
      "89 : 10\n",
      "100 : 9\n",
      "88 : 6\n",
      "58 : 7\n",
      "87 : 12\n",
      "5 : 9\n",
      "18 : 17\n",
      "29 : 14\n",
      "66 : 15\n",
      "35 : 12\n",
      "19 : 14\n",
      "52 : 18\n",
      "56 : 12\n",
      "56 : 12\n",
      "25 : 12\n",
      "16 : 6\n",
      "95 : 10\n",
      "84 : 12\n",
      "79 : 11\n",
      "81 : 10\n",
      "45 : 9\n",
      "97 : 21\n",
      "78 : 12\n",
      "66 : 15\n",
      "36 : 7\n",
      "38 : 11\n",
      "63 : 8\n",
      "10 : 13\n",
      "27 : 5\n",
      "77 : 15\n",
      "45 : 9\n",
      "78 : 12\n",
      "7 : 11\n",
      "51 : 12\n",
      "4 : 7\n",
      "25 : 12\n",
      "31 : 8\n",
      "13 : 11\n",
      "61 : 6\n",
      "92 : 8\n",
      "65 : 12\n",
      "7 : 11\n",
      "13 : 11\n",
      "55 : 7\n",
      "21 : 8\n",
      "71 : 7\n",
      "66 : 15\n",
      "40 : 16\n",
      "42 : 10\n",
      "97 : 21\n",
      "56 : 12\n",
      "88 : 6\n",
      "18 : 17\n",
      "66 : 15\n",
      "77 : 15\n",
      "75 : 9\n",
      "97 : 21\n",
      "90 : 13\n",
      "10 : 13\n",
      "18 : 17\n",
      "47 : 8\n",
      "29 : 14\n",
      "61 : 6\n",
      "1 : 8\n",
      "65 : 12\n",
      "84 : 12\n",
      "100 : 9\n",
      "37 : 10\n",
      "6 : 7\n",
      "88 : 6\n",
      "54 : 8\n",
      "14 : 14\n",
      "20 : 10\n",
      "40 : 16\n",
      "85 : 9\n",
      "14 : 14\n",
      "63 : 8\n",
      "26 : 11\n",
      "5 : 9\n",
      "31 : 8\n",
      "61 : 6\n",
      "64 : 4\n",
      "51 : 12\n",
      "2 : 13\n",
      "89 : 10\n",
      "33 : 10\n",
      "43 : 9\n",
      "25 : 12\n",
      "14 : 14\n",
      "69 : 2\n",
      "58 : 7\n",
      "16 : 6\n",
      "53 : 12\n",
      "84 : 12\n",
      "2 : 13\n",
      "17 : 11\n",
      "29 : 14\n",
      "54 : 8\n",
      "96 : 9\n",
      "59 : 10\n",
      "96 : 9\n",
      "34 : 8\n",
      "18 : 17\n",
      "56 : 12\n",
      "65 : 12\n",
      "78 : 12\n",
      "24 : 12\n",
      "29 : 14\n",
      "68 : 6\n",
      "41 : 11\n",
      "64 : 4\n",
      "30 : 9\n",
      "81 : 10\n",
      "16 : 6\n",
      "70 : 15\n",
      "35 : 12\n",
      "85 : 9\n",
      "52 : 18\n",
      "60 : 12\n",
      "70 : 15\n",
      "14 : 14\n",
      "24 : 12\n",
      "84 : 12\n",
      "36 : 7\n",
      "0 : 10\n",
      "54 : 8\n",
      "34 : 8\n",
      "48 : 9\n",
      "55 : 7\n",
      "62 : 8\n",
      "75 : 9\n",
      "39 : 13\n",
      "89 : 10\n",
      "36 : 7\n",
      "35 : 12\n",
      "26 : 11\n",
      "40 : 16\n",
      "40 : 16\n",
      "18 : 17\n",
      "71 : 7\n",
      "68 : 6\n",
      "83 : 13\n",
      "30 : 9\n",
      "65 : 12\n",
      "56 : 12\n",
      "20 : 10\n",
      "96 : 9\n",
      "24 : 12\n",
      "18 : 17\n",
      "38 : 11\n",
      "59 : 10\n",
      "93 : 11\n",
      "33 : 10\n",
      "35 : 12\n",
      "23 : 6\n",
      "27 : 5\n",
      "6 : 7\n",
      "44 : 7\n",
      "52 : 18\n",
      "6 : 7\n",
      "92 : 8\n",
      "91 : 11\n",
      "20 : 10\n",
      "28 : 12\n",
      "80 : 9\n",
      "18 : 17\n",
      "99 : 11\n",
      "31 : 8\n",
      "80 : 9\n",
      "9 : 9\n",
      "18 : 17\n",
      "66 : 15\n",
      "95 : 10\n",
      "66 : 15\n",
      "42 : 10\n",
      "29 : 14\n",
      "59 : 10\n",
      "49 : 10\n",
      "26 : 11\n",
      "10 : 13\n",
      "35 : 12\n",
      "57 : 10\n",
      "94 : 13\n",
      "51 : 12\n",
      "4 : 7\n",
      "12 : 8\n",
      "8 : 12\n",
      "39 : 13\n",
      "83 : 13\n",
      "37 : 10\n",
      "38 : 11\n",
      "52 : 18\n",
      "63 : 8\n",
      "7 : 11\n",
      "33 : 10\n",
      "68 : 6\n",
      "66 : 15\n",
      "93 : 11\n",
      "40 : 16\n",
      "33 : 10\n",
      "43 : 9\n",
      "77 : 15\n",
      "5 : 9\n",
      "18 : 17\n",
      "48 : 9\n",
      "81 : 10\n",
      "35 : 12\n",
      "91 : 11\n",
      "51 : 12\n",
      "24 : 12\n",
      "57 : 10\n",
      "10 : 13\n",
      "62 : 8\n",
      "44 : 7\n",
      "77 : 15\n",
      "40 : 16\n",
      "70 : 15\n",
      "69 : 2\n",
      "14 : 14\n",
      "47 : 8\n",
      "39 : 13\n",
      "86 : 16\n",
      "0 : 10\n",
      "25 : 12\n",
      "97 : 21\n",
      "39 : 13\n",
      "45 : 9\n",
      "40 : 16\n",
      "49 : 10\n",
      "65 : 12\n",
      "53 : 12\n",
      "89 : 10\n",
      "53 : 12\n",
      "70 : 15\n",
      "90 : 13\n",
      "17 : 11\n",
      "57 : 10\n",
      "48 : 9\n",
      "67 : 7\n",
      "30 : 9\n",
      "93 : 11\n",
      "34 : 8\n",
      "24 : 12\n",
      "78 : 12\n",
      "21 : 8\n",
      "73 : 9\n",
      "43 : 9\n",
      "95 : 10\n",
      "19 : 14\n",
      "67 : 7\n",
      "98 : 9\n",
      "28 : 12\n",
      "92 : 8\n",
      "14 : 14\n",
      "83 : 13\n",
      "72 : 6\n",
      "41 : 11\n",
      "96 : 9\n",
      "89 : 10\n",
      "97 : 21\n",
      "29 : 14\n",
      "51 : 12\n",
      "26 : 11\n",
      "15 : 9\n",
      "39 : 13\n",
      "22 : 9\n",
      "15 : 9\n",
      "8 : 12\n",
      "27 : 5\n",
      "25 : 12\n",
      "73 : 9\n",
      "71 : 7\n",
      "93 : 11\n",
      "86 : 16\n",
      "63 : 8\n",
      "22 : 9\n",
      "96 : 9\n",
      "78 : 12\n",
      "77 : 15\n",
      "4 : 7\n",
      "24 : 12\n",
      "35 : 12\n",
      "5 : 9\n",
      "52 : 18\n",
      "41 : 11\n",
      "8 : 12\n",
      "57 : 10\n",
      "40 : 16\n",
      "93 : 11\n",
      "26 : 11\n",
      "51 : 12\n",
      "19 : 14\n",
      "68 : 6\n",
      "32 : 8\n",
      "62 : 8\n",
      "15 : 9\n",
      "78 : 12\n",
      "97 : 21\n",
      "94 : 13\n",
      "82 : 8\n",
      "53 : 12\n",
      "3 : 12\n",
      "44 : 7\n",
      "11 : 9\n",
      "97 : 21\n",
      "8 : 12\n",
      "58 : 7\n",
      "20 : 10\n",
      "81 : 10\n",
      "89 : 10\n",
      "87 : 12\n",
      "8 : 12\n",
      "51 : 12\n",
      "39 : 13\n",
      "46 : 2\n",
      "68 : 6\n",
      "52 : 18\n",
      "99 : 11\n",
      "86 : 16\n",
      "83 : 13\n",
      "18 : 17\n",
      "98 : 9\n",
      "17 : 11\n",
      "54 : 8\n",
      "82 : 8\n",
      "5 : 9\n",
      "83 : 13\n",
      "78 : 12\n",
      "97 : 21\n",
      "83 : 13\n",
      "21 : 8\n",
      "48 : 9\n",
      "91 : 11\n",
      "92 : 8\n",
      "57 : 10\n",
      "73 : 9\n",
      "79 : 11\n",
      "3 : 12\n",
      "39 : 13\n",
      "56 : 12\n",
      "91 : 11\n",
      "49 : 10\n",
      "11 : 9\n",
      "2 : 13\n",
      "56 : 12\n",
      "65 : 12\n",
      "84 : 12\n",
      "97 : 21\n",
      "5 : 9\n",
      "94 : 13\n",
      "10 : 13\n",
      "85 : 9\n",
      "98 : 9\n",
      "71 : 7\n",
      "63 : 8\n",
      "97 : 21\n",
      "32 : 8\n",
      "50 : 4\n",
      "60 : 12\n",
      "90 : 13\n",
      "74 : 4\n",
      "47 : 8\n",
      "67 : 7\n",
      "85 : 9\n",
      "12 : 8\n",
      "49 : 10\n",
      "56 : 12\n",
      "80 : 9\n",
      "17 : 11\n",
      "92 : 8\n",
      "7 : 11\n",
      "20 : 10\n",
      "37 : 10\n",
      "77 : 15\n",
      "87 : 12\n",
      "26 : 11\n",
      "88 : 6\n",
      "86 : 16\n",
      "49 : 10\n",
      "19 : 14\n",
      "13 : 11\n",
      "48 : 9\n",
      "100 : 9\n",
      "1 : 8\n",
      "80 : 9\n",
      "80 : 9\n",
      "89 : 10\n",
      "3 : 12\n",
      "28 : 12\n",
      "79 : 11\n",
      "79 : 11\n",
      "37 : 10\n",
      "2 : 13\n",
      "68 : 6\n",
      "35 : 12\n",
      "98 : 9\n",
      "2 : 13\n",
      "92 : 8\n",
      "96 : 9\n",
      "10 : 13\n",
      "5 : 9\n",
      "62 : 8\n",
      "92 : 8\n",
      "10 : 13\n",
      "58 : 7\n",
      "83 : 13\n",
      "84 : 12\n",
      "90 : 13\n",
      "82 : 8\n",
      "83 : 13\n",
      "8 : 12\n",
      "14 : 14\n",
      "13 : 11\n",
      "63 : 8\n",
      "87 : 12\n",
      "43 : 9\n",
      "56 : 12\n",
      "40 : 16\n",
      "33 : 10\n",
      "62 : 8\n",
      "78 : 12\n",
      "41 : 11\n",
      "23 : 6\n",
      "90 : 13\n",
      "12 : 8\n",
      "87 : 12\n",
      "34 : 8\n",
      "99 : 11\n",
      "54 : 8\n",
      "1 : 8\n",
      "81 : 10\n",
      "90 : 13\n",
      "8 : 12\n",
      "41 : 11\n",
      "91 : 11\n",
      "100 : 9\n",
      "4 : 7\n",
      "61 : 6\n",
      "67 : 7\n",
      "99 : 11\n",
      "82 : 8\n",
      "89 : 10\n",
      "42 : 10\n",
      "66 : 15\n",
      "97 : 21\n",
      "70 : 15\n",
      "41 : 11\n",
      "32 : 8\n",
      "9 : 9\n",
      "59 : 10\n",
      "90 : 13\n",
      "75 : 9\n",
      "60 : 12\n",
      "92 : 8\n",
      "9 : 9\n",
      "67 : 7\n",
      "65 : 12\n",
      "11 : 9\n",
      "39 : 13\n",
      "97 : 21\n",
      "60 : 12\n",
      "21 : 8\n",
      "90 : 13\n",
      "97 : 21\n",
      "9 : 9\n",
      "63 : 8\n",
      "0 : 10\n",
      "96 : 9\n",
      "74 : 4\n",
      "32 : 8\n",
      "37 : 10\n",
      "34 : 8\n",
      "29 : 14\n",
      "100 : 9\n",
      "70 : 15\n",
      "13 : 11\n",
      "44 : 7\n",
      "85 : 9\n",
      "52 : 18\n",
      "38 : 11\n",
      "84 : 12\n",
      "78 : 12\n",
      "78 : 12\n",
      "53 : 12\n",
      "90 : 13\n",
      "72 : 6\n",
      "81 : 10\n",
      "94 : 13\n",
      "91 : 11\n",
      "95 : 10\n",
      "65 : 12\n",
      "0 : 10\n",
      "20 : 10\n",
      "24 : 12\n",
      "35 : 12\n",
      "100 : 9\n",
      "82 : 8\n",
      "79 : 11\n",
      "10 : 13\n",
      "84 : 12\n",
      "79 : 11\n",
      "13 : 11\n",
      "53 : 12\n",
      "96 : 9\n",
      "31 : 8\n",
      "38 : 11\n",
      "48 : 9\n",
      "94 : 13\n",
      "26 : 11\n",
      "40 : 16\n",
      "94 : 13\n",
      "38 : 11\n",
      "22 : 9\n",
      "3 : 12\n",
      "99 : 11\n",
      "76 : 6\n",
      "30 : 9\n",
      "25 : 12\n",
      "75 : 9\n",
      "19 : 14\n",
      "34 : 8\n",
      "60 : 12\n",
      "79 : 11\n",
      "58 : 7\n",
      "74 : 4\n",
      "2 : 13\n",
      "80 : 9\n",
      "62 : 8\n",
      "17 : 11\n",
      "91 : 11\n",
      "49 : 10\n",
      "36 : 7\n",
      "30 : 9\n",
      "54 : 8\n",
      "31 : 8\n",
      "53 : 12\n",
      "4 : 7\n",
      "5 : 9\n",
      "19 : 14\n",
      "66 : 15\n",
      "89 : 10\n",
      "72 : 6\n",
      "90 : 13\n",
      "57 : 10\n",
      "19 : 14\n",
      "1 : 8\n",
      "8 : 12\n",
      "83 : 13\n",
      "28 : 12\n",
      "88 : 6\n",
      "9 : 9\n",
      "20 : 10\n",
      "55 : 7\n",
      "60 : 12\n",
      "70 : 15\n",
      "3 : 12\n",
      "65 : 12\n",
      "31 : 8\n",
      "1 : 8\n",
      "97 : 21\n",
      "2 : 13\n",
      "84 : 12\n",
      "31 : 8\n",
      "57 : 10\n",
      "97 : 21\n",
      "39 : 13\n",
      "14 : 14\n",
      "91 : 11\n",
      "65 : 12\n",
      "45 : 9\n",
      "97 : 21\n",
      "76 : 6\n",
      "41 : 11\n",
      "49 : 10\n",
      "51 : 12\n",
      "3 : 12\n",
      "2 : 13\n",
      "94 : 13\n",
      "70 : 15\n",
      "80 : 9\n",
      "23 : 6\n",
      "10 : 13\n",
      "25 : 12\n",
      "75 : 9\n",
      "55 : 7\n",
      "52 : 18\n",
      "40 : 16\n",
      "14 : 14\n",
      "38 : 11\n",
      "54 : 8\n",
      "78 : 12\n",
      "75 : 9\n",
      "55 : 7\n",
      "52 : 18\n",
      "86 : 16\n",
      "37 : 10\n",
      "11 : 9\n",
      "75 : 9\n",
      "3 : 12\n",
      "70 : 15\n",
      "17 : 11\n",
      "86 : 16\n",
      "56 : 12\n",
      "19 : 14\n",
      "94 : 13\n",
      "13 : 11\n",
      "82 : 8\n",
      "63 : 8\n",
      "86 : 16\n",
      "84 : 12\n",
      "42 : 10\n",
      "47 : 8\n",
      "96 : 9\n",
      "32 : 8\n",
      "16 : 6\n",
      "13 : 11\n",
      "29 : 14\n",
      "22 : 9\n",
      "14 : 14\n",
      "42 : 10\n",
      "87 : 12\n",
      "90 : 13\n",
      "11 : 9\n",
      "58 : 7\n",
      "79 : 11\n",
      "44 : 7\n",
      "4 : 7\n",
      "86 : 16\n",
      "25 : 12\n",
      "86 : 16\n",
      "5 : 9\n",
      "60 : 12\n",
      "99 : 11\n",
      "77 : 15\n",
      "66 : 15\n",
      "81 : 10\n",
      "38 : 11\n",
      "38 : 11\n",
      "22 : 9\n",
      "81 : 10\n",
      "14 : 14\n",
      "77 : 15\n",
      "33 : 10\n",
      "45 : 9\n",
      "28 : 12\n",
      "1 : 8\n",
      "55 : 7\n",
      "66 : 15\n",
      "71 : 7\n",
      "67 : 7\n",
      "94 : 13\n",
      "84 : 12\n",
      "33 : 10\n",
      "52 : 18\n",
      "56 : 12\n",
      "29 : 14\n",
      "50 : 4\n",
      "6 : 7\n",
      "82 : 8\n",
      "50 : 4\n",
      "13 : 11\n",
      "47 : 8\n",
      "55 : 7\n",
      "86 : 16\n",
      "27 : 5\n",
      "65 : 12\n",
      "26 : 11\n",
      "95 : 10\n",
      "11 : 9\n",
      "47 : 8\n",
      "19 : 14\n",
      "51 : 12\n",
      "3 : 12\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x = [random.randint(0,100)for i in range(1000)]\n",
    "print(x)\n",
    "for i in x:\n",
    "    print(i,':',x.count(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.为什么应尽量从列表的尾部进行元素的增加与删除操作?提高运作速度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.编写程序，生成包含1000个0-100的随机整数，并统计每个元素的出现次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 7\n",
      "1 : 10\n",
      "2 : 6\n",
      "3 : 9\n",
      "4 : 7\n",
      "5 : 8\n",
      "6 : 7\n",
      "7 : 9\n",
      "8 : 4\n",
      "9 : 7\n",
      "10 : 12\n",
      "11 : 13\n",
      "12 : 10\n",
      "13 : 8\n",
      "14 : 12\n",
      "15 : 8\n",
      "16 : 10\n",
      "17 : 8\n",
      "18 : 6\n",
      "19 : 8\n",
      "20 : 6\n",
      "21 : 14\n",
      "22 : 6\n",
      "23 : 8\n",
      "24 : 9\n",
      "25 : 8\n",
      "26 : 13\n",
      "27 : 11\n",
      "28 : 10\n",
      "29 : 9\n",
      "30 : 13\n",
      "31 : 14\n",
      "32 : 11\n",
      "33 : 10\n",
      "34 : 13\n",
      "35 : 7\n",
      "36 : 12\n",
      "37 : 13\n",
      "38 : 9\n",
      "39 : 5\n",
      "40 : 14\n",
      "41 : 18\n",
      "42 : 10\n",
      "43 : 7\n",
      "44 : 10\n",
      "45 : 12\n",
      "46 : 12\n",
      "47 : 8\n",
      "48 : 7\n",
      "49 : 8\n",
      "50 : 11\n",
      "51 : 15\n",
      "52 : 9\n",
      "53 : 8\n",
      "54 : 14\n",
      "55 : 15\n",
      "56 : 10\n",
      "57 : 7\n",
      "58 : 12\n",
      "59 : 9\n",
      "60 : 11\n",
      "61 : 11\n",
      "62 : 8\n",
      "63 : 12\n",
      "64 : 8\n",
      "65 : 12\n",
      "66 : 15\n",
      "67 : 7\n",
      "68 : 8\n",
      "69 : 13\n",
      "70 : 5\n",
      "71 : 16\n",
      "72 : 9\n",
      "73 : 13\n",
      "74 : 14\n",
      "75 : 15\n",
      "76 : 8\n",
      "77 : 12\n",
      "78 : 12\n",
      "79 : 10\n",
      "80 : 10\n",
      "81 : 7\n",
      "82 : 7\n",
      "83 : 13\n",
      "84 : 9\n",
      "85 : 8\n",
      "86 : 11\n",
      "87 : 11\n",
      "88 : 7\n",
      "89 : 19\n",
      "90 : 11\n",
      "91 : 9\n",
      "92 : 5\n",
      "93 : 9\n",
      "94 : 12\n",
      "95 : 9\n",
      "96 : 6\n",
      "97 : 7\n",
      "98 : 8\n",
      "99 : 8\n",
      "100 : 9\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x=[random.randint(0,100)for i in range(1000)]\n",
    "d=set(x)\n",
    "for v in d:\n",
    "    print(v,':',x.count(v))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16.编写程序，用户输入一个列表和2个整数作为下标，然后输出列表中介于2个下标闭区间之间的元素组成的子列表。例如用户输入[1,2,3,4,5,6]和2,5，程序输出[3,4,5,6]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表：3,2,5,6,\n",
      "请输入开始位置和末尾位置：2,4\n",
      "(5, 6)\n"
     ]
    }
   ],
   "source": [
    "x=eval(input('请输入一个列表：'))\n",
    "start,end=eval(input('请输入开始位置和末尾位置：'))\n",
    "print(x[start:end+1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.设计一个字典，并编写程序，用户输入内容作为“键”，然后输出字典中对应的“值”，如果用户输入的“键”不存在，则输出“您输入的不存在！”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个键:3\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "d={1:'a',2:'b',3:'c',4:'d'}\n",
    "v=eval(input('请输入一个键:'))\n",
    "print(d.get(v,'您输入的键不存在'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18.编写程序，生成包含20个随机数的列表，然后将前10个元素升序排列，后10个元素降序排列，并输出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88, 28, 73, 60, 76, 56, 26, 3, 95, 64, 51, 27, 47, 45, 92, 47, 26, 34, 97, 70]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x=[random.randint(0,100)for i in range(20)]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 26, 28, 56, 60, 64, 73, 76, 88, 95, 97, 92, 70, 51, 47, 47, 45, 34, 27, 26]\n"
     ]
    }
   ],
   "source": [
    "y=x[0:10]\n",
    "y.sort()\n",
    "x[0:10]=y\n",
    "y=x[10:20]\n",
    "y.sort(reverse=True)\n",
    "x[10:20]=y\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
